<template>
  <div>
    <div class="shopcart">
    <div class="content" @click="toggleList">
      <div class="content-left">
        <div class="logo-wrapper">
          <div class="logo" :class="{'highlight':totalCount > 0}">
            <span
              class="iconfont icon-shopping_cart"
              :class="{'highlight':totalCount > 0}"
            ></span>
          </div>
          <div class="num" v-show="totalCount > 0">{{totalCount}} </div>
        </div>
        <div class="price border-right" :class="{'highlight':totalPrice > 0}">¥
          {{totalPrice}}</div>
        <div class="desc">另需配送费¥{{deliveryPrice}}元</div>
      </div>
      <div class="content-right" @click.stop.prevent="pay">
        <div class="pay" :class="{'highlight':totalPrice >= this.minPrice}">
          {{payDesc}}
        </div>
      </div>
    </div>
    <div class="ball-container">
        <div
          v-for="(ball,index) in balls"
          :key="index"
        >
          <transition
            v-on:before-enter="beforeDrop"
            v-on:enter="dropping"
            v-on:after-enter="afterDrop"
          >
            <div class="ball" v-show="ball.show">
              <div class="inner inner-hook"></div>
            </div>
          </transition>
        </div>
      </div>
    <transition name="fold">
      <div class="shopcart-list" v-show="listShow" >
      <div class="list-header border-bottom">
        <h1 class="title">购物车</h1>
        <span class="empty" @click="empty">清空</span>
      </div>
      <div class="list-content" ref="listContent">
        <ul>
          <li class="food border-bottom" v-for="(food,index) in selectFoods" :key="index">
            <span class="name">{{food.name}}</span>
            <div class="price">
              <span>¥{{food.price * food.count}}</span>
            </div>
            <div class="cartcontrol-wrapper">
              <cart-control :food="food"></cart-control>
            </div>
          </li>
        </ul>
      </div>
    </div>
    </transition>
  </div>
    <transition name="fade">
      <div class="list-mask" v-show="listShow" @click="hideList"></div>
    </transition>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import cartControl from '../cartcontrol/cartcontrol.vue'
export default {
  name: 'shopcart',
  props: {
    selectFoods: {
      type: Array,
      default () {
        return [
          {
            price: 10,
            count: 12
          }
        ]
      }
    },
    deliveryPrice: Number,
    minPrice: Number
  },
  components: {
    cartControl
  },
  data () {
    return {
      fold: true,
      balls: [
        {
          show: false
        },
        {
          show: false
        },
        {
          show: false
        },
        {
          show: false
        },
        {
          show: false
        }
      ],
      dropBalls: []
    }
  },
  computed: {
    totalPrice () {
      let total = 0
      this.selectFoods.forEach((food) => {
        total += food.price * food.count
      })
      return total
    },
    totalCount () {
      let count = 0
      this.selectFoods.forEach((food) => {
        count += food.count
      })
      return count
    },
    payDesc () {
      if (this.totalPrice === 0) {
        return `¥${this.minPrice}元起送`
      } else if (this.totalPrice < this.minPrice) {
        let diff = this.minPrice - this.totalPrice
        return `还差¥${diff}元起送`
      } else {
        return '去结算'
      }
    },
    listShow () {
      /*
      this指向问题
      */
      const _this = this
      if (!this.totalCount) {
        _this.fold = true
        return false
      }
      let show = !this.fold
      if (show) {
        this.$nextTick(() => {
          if (!this.scroll) {
            _this.scroll = new Bscroll(this.$refs.listContent)
          } else {
            this.scroll.refresh()
          }
        })
      }
      return show
    }
    /*
    listShow: {
      get: function () {
        return this.fold
      },
      set: function () {
        if (!this.totalCount) {
          this.fold = true
          return false
        }
        let show = !this.fold
        return show
      }
    }
    */
  },
  methods: {
    toggleList () {
      if (!this.totalCount) {
        return
      }
      this.fold = !this.fold
    },
    empty () {
      this.selectFoods.forEach((food) => {
        food.count = 0
      })
    },
    hideList () {
      this.fold = true
    },
    pay () {
      if (this.totalPrice < this.minPrice) {
        return
      }
      alert(`支付${this.totalPrice}元`)
    },
    drop (el) {
      for (let i = 0; i < this.balls.length; i++) {
        let ball = this.balls[i]
        if (!this.show) {
          ball.show = true
          ball.el = el
          this.dropBalls.push(ball)
          return
        }
      }
    },
    beforeDrop (el) {
      let count = this.balls.length
      while (count--) {
        let ball = this.balls[count]
        if (ball.show) {
          // 元素相对于视口的距离
          let rect = ball.el.getBoundingClientRect()

          // x,y 为初始点 与 目标点的差值
          let x = rect.left - 32
          let y = -(window.innerHeight - rect.top - 22)
          // 让其显示
          el.style.display = ''

          // el （初始位置为 0，0，0）和购物车icon在一起，将小球（el）  放到加号位置去
          // 纵向动画
          el.style.webkitTransform = `translate3d(0,${y}px,0)`
          el.style.transform = `translate3d(0,${y}px,0)`

          // el.style.webkitTransform = `translate3d(0,${x}px,0)`;
          // el.style.transform = `translate3d(0,${x}px,0)`;

          // 横向动画  inner-hook，  仅仅定义类 dom选择器，不做样式
          let inner = el.getElementsByClassName('inner-hook')[0]
          inner.style.webkitTransform = `translate3d(${x}px,0,0)`
          inner.style.transform = `translate3d(${x}px,0,0)`
        }
      }
    },
    dropping (el, done) {
      // 手动触发浏览器重绘，便于translate3d，--rf 变量不会使用
      /* eslint-disable no-unused-vars */
      let rf = el.offsetHeight

      this.$nextTick(() => {
        // 小球样式位置，置于购物车按钮位置处
        el.style.webkitTransform = 'translate3d(0,0,0)'
        el.style.transform = 'translate3d(0,0,0)'

        let inner = el.getElementsByClassName('inner-hook')[0]
        inner.style.webkitTransform = 'translate3d(0,0,0)'
        inner.style.transform = 'translate3d(0,0,0)'

        el.addEventListener('transitionend', done)
      })
    },
    afterDrop (el) {
      // 此轮动画结束后，将此次的 ball 取出 ，ball状态重置，，el display:none
      let ball = this.dropBalls.shift()
      if (ball) {
        ball.show = false
        el.style.display = 'none'
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  .shopcart
    position fixed
    left 0
    bottom 0
    z-index 50
    width 100%
    height .96rem
    .content
      display flex
      background #141d27
      font-size 0
      .content-left
        flex 1
        .logo-wrapper
          position relative
          display inline-block
          vertical-align top
          top -.2rem
          margin 0 .24rem
          padding .12rem
          width 1.12rem
          height 1.12rem
          //IE的模盒型
          box-sizing border-box
          border-radius 50%
          background #141d27
          .logo
            width 100%
            height 100%
            vertical-align top
            border-radius 50%
            text-align center
            background #2b343c
            &.highlight
              background rgb(0,160,220)
            .icon-shopping_cart
              line-height .88rem
              font-size .48rem
              color #80858a
              &.highlight
                color white
          .num
            position absolute
            top 0
            right 0
            width .48rem
            height .32rem
            line-height .32rem
            text-align center
            border-radius .32rem
            font-size .18rem
            font-weight 700
            color #ffffff
            background rgb(240,20,20)
            box-shadow 0 .08rem .16rem 0 rgba(0,0,0,0.4)
        .price
          display inline-block
          vertical-align top
          margin-top .24rem
          line-height .48rem
          padding-right .24rem
          //ie盒模型
          box-sizing border-box
          font-size .32rem
          font-weight 700
          color rgba(255,255,255,0.4)
          &.highlight
            color #ffffff
        .border-right::before
          background white
          opacity 0.1
        .desc
          display inline-block
          vertical-align top
          margin-top .24rem
          margin-left .24rem
          line-height .48rem
          font-size .24rem
          color rgba(255,255,255,0.4)
      .content-right
        flex 0 0 2.1rem
        width 2.1rem
        .pay
          height .96rem
          line-height .96rem
          text-align center
          font-size .24rem
          font-weight 700
          color rgba(255,255,255,0.4)
          background #2b333b
          &.highlight
            background #00b43c
            color #ffffff
    .ball-container
      .ball
        position fixed
        left .64rem
        bottom .44rem
        z-index 200
        // y轴，贝塞尔曲线
        transition:all 0.5s cubic-bezier(0.49, -0.29, 0.75, 0.41)
        .inner
          width .32rem
          height .32rem
          border-radius 50%
          background rgb(0,160,220)
          // x轴只需要线性缓动
          transition:all 0.5s linear
    .shopcart-list
      z-index -1
      position absolute
      top 0
      left 0
      right 0
      width 100%
      transform translate3d(0,-100%,0)
      &.fold-enter-active, &.fold-leave-active
        transition :all 0.4s linear
      &.fold-enter, &.fold-leave-active
        transform :translate3d(0,0,0)
      .list-header
        line-height .8rem
        height .8rem
        padding 0 .36rem
        background #f3f5f7
        .title
          float left
          font-size .28rem
          font-weight 200
          color rbg(7,17,27)
        .empty
          float right
          font-size .24rem
          color rgb(0,160,220)
      .list-content
        overflow hidden
        padding 0 0.36rem
        max-height 4.34rem
        background #ffffff
        .food
          position relative
          padding .24rem 0
          box-sizing border-box
          &.border-bottom::before
            background black
            opacity 0.2
          .name
            line-height .48rem
            font-size .28rem
            color rgb(7,17,27)
          .price
            position absolute
            right 1.8rem
            bottom .24rem
            line-height .48rem
            font-size .28rem
            font-weight 700
            color rgb(240,20,20)
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom .12rem
  .list-mask
    z-index 40
    position fixed
    top 0
    left 0
    width 100%
    height 100%
    background rgba(7,17,27,.6)
    &.fade-enter-active, &.fade-leave-active
      transition all .5s
    &.fade-enter, &.fade-leave-to
      opacity 0
      background rgba(7,17,27,0)
</style>
